<template>
  <div class="xyq_search">
    <div class="main">
      <div class="main_top">
        <div class="select_box">
          <el-select
            @change="changeArea"
            filterable
            v-model="areaValue"
            placeholder="请选择大区"
          >
            <el-option
              v-for="item in areaServerList"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
          <el-select
            filterable
            :disabled="!areaValue"
            v-model="serverValue"
            placeholder="请选择服务器"
          >
            <el-option
              v-for="item in serverList"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </div>
        <el-button @click="search" type="primary">搜索</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        style="width: 100%"
      >
        <el-table-column align="center" prop="area" label="大区">
        </el-table-column>
        <el-table-column align="center" prop="server" label="服务器">
        </el-table-column>
      </el-table>
      <!-- <div  class="server_list" >
          <div v-for="(item,index) in tableData" :key="index" class="server_list_item" >
              {{item}}
          </div>
      </div> -->
    </div>
  </div>
</template>

<script>
import { getXyqServerList, getXyqTraList } from '@/api'
export default {
  data () {
    return {
      serverObj: {},
      traObj: {},
      tableData: [],
      areaObj: {},
      areaValue: '',
      serverValue: '',
      loading: false
    }
  },
  computed: {
    areaServerList () {
      const list = []
      for (const key in this.serverObj) {
        if (key) {
          list.push(key)
        }
      }
      return list
    },
    serverList () {
      return this.serverObj[this.areaValue] || []
    }
  },
  mounted () {
    this.getXyqServerList()
    this.getXyqTraList()
  },
  methods: {
    async getXyqServerList () {
      try {
        const res = await getXyqServerList()
        if (res.data.errno === 0) {
          this.serverObj = res.data.data.serverList
          this.areaObj = res.data.data.areaList
        }
      } catch (error) {
        this.$errAlert(error)
      }
    },
    async getXyqTraList () {
      try {
        const res = await getXyqTraList()
        if (res.data.errno === 0) {
          this.traObj = res.data.data
        }
      } catch (error) {
        this.$errAlert(error)
      }
    },
    changeArea () {
      this.serverValue = ''
    },
    search () {
      if (!this.serverValue) {
        this.$message({
          message: '请先选择服务器',
          type: 'warning'
        })
        return
      }
      this.loading = true
      const serverList = this.traObj[this.serverValue]
      if (!serverList || !serverList.length) {
        this.tableData = []
        this.$message({
          message: '该服务器暂无可转出的服务器',
          type: 'warning'
        })
        this.loading = false
        return
      }
      const list = []
      serverList.forEach((element) => {
        const obj = {}
        obj.area = this.areaObj[element]
        obj.server = element
        list.push(obj)
      })
      this.tableData = list
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.xyq_search {
  width: 100%;
  height: 100%;
  .main {
    width: 90%;
    height: 100%;
    margin: 50px auto;
    .main_top {
      display: flex;
      justify-content: space-between;
    }
    .server_list {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      .server_list_item {
        width: 80px;
        height: 40px;
        line-height: 40px;
      }
    }
  }
}
</style>
